<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class QrcodeController extends BaseController
{

    public function store(Request $request) {
        $this->validate($request, [
//            'id' => 'required',
            'scene' => 'required',
            'page' => 'required',
        ]);
        $app = $this->miniProgram();
//        $id = $request->input('id');
        $scene = \request('scene');
        $page = \request('page');
        $key = substr(md5($scene . $page), 8, -8);
        $fileName = 'appcode_pet_' . $key . '.png';
        if (file_exists('qrcode/' . $fileName)) {
            return $this->success([
                'url' => env('APP_URL') . '/qrcode/' . $fileName
            ]);
        } else {
            $response = $app->app_code->getUnlimit($scene, [
                'page' => $page
            ]);
            if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $fileName = $response->saveAs('qrcode', $fileName);
                return $this->success([
                    'url' => env('APP_URL') . '/qrcode/' . $fileName
                ]);
            } else {
                return $this->failed('生成二维码失败');
            }
        }
    }



}

